﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfRestSample
{   
    public class BookService : IBookService
    {
        public List<Objeto> GG(string id,string otro)
        {
            List<Objeto> lista = new List<Objeto>();
            lista.Add(new Objeto("pepe",Int32.Parse(id)));
            lista.Add(new Objeto("juan",Int32.Parse(otro)));
            return lista;
        }
        public ListaGeneric<DIR> getDIRS(string fecha)
        {
            //Prueba p = new Prueba();
            ListaGeneric<DIR> p = new ListaGeneric<DIR>();
            p.add(new DIR { codDIR = "dir0001",codLocal="local1"});
            p.add(new DIR { codDIR = "dir0002", codLocal = "local2" });
            return p;
        }

        public ListaGeneric<MotivoModelService> getMotivos(string fecha)
        {
            ListaGeneric<MotivoModelService> l = new ListaGeneric<MotivoModelService>();
            l.add(new MotivoModelService { codMotivo="cod1",motivo="motivo1"});
            l.add(new MotivoModelService { codMotivo = "cod2", motivo = "motivo2" });
            return l;
        }
        public ListaGeneric<RepresentanteComercialModelService> getRepresentanteComerciales(string fecha)
        {
            ListaGeneric<RepresentanteComercialModelService> l = new ListaGeneric<RepresentanteComercialModelService>();
            l.add(new RepresentanteComercialModelService { nombres="pedro",apellidos="ramirez",codRepresentante="cod1" });
            l.add(new RepresentanteComercialModelService { nombres = "alexander", apellidos = "labajos", codRepresentante = "cod2" });
            return l;
        }
        
        public List<Book> GetBooksList()
        {
            using (SampleDbEntities entities = new SampleDbEntities())
            {
                return entities.Books.ToList();
            }
        }

        public Book GetBookById(string id)
        {
            try
            {
                int bookId = Convert.ToInt32(id);

                using (SampleDbEntities entities = new SampleDbEntities())
                {
                    return entities.Books.SingleOrDefault(book => book.ID == bookId);
                }
            }
            catch
            {
                throw new FaultException("Something went wrong");
            }
        }

        public void AddBook(string name)
        {
            using (SampleDbEntities entities = new SampleDbEntities())
            {
                Book book = new Book { BookName = name };
                entities.Books.AddObject(book);
                entities.SaveChanges();
            }
        }

        public void UpdateBook(string id, string name)
        {
            try
            {
                int bookId = Convert.ToInt32(id);

                using (SampleDbEntities entities = new SampleDbEntities())
                {
                    Book book = entities.Books.SingleOrDefault(b => b.ID == bookId);
                    book.BookName = name;
                    entities.SaveChanges();
                }
            }
            catch
            {
                throw new FaultException("Something went wrong");
            }
        }

        public void DeleteBook(string id)
        {
            try
            {
                int bookId = Convert.ToInt32(id);

                using (SampleDbEntities entities = new SampleDbEntities())
                {
                    Book book = entities.Books.SingleOrDefault(b => b.ID == bookId);
                    entities.Books.DeleteObject(book);
                    entities.SaveChanges();
                }
            }
            catch
            {
                throw new FaultException("Something went wrong");
            }
        }

        public List<string> GetBooksNames()
        {
            using (SampleDbEntities entities = new SampleDbEntities())
            {
                return entities.Books.Select(book => book.BookName).ToList();
            }
        }
    }
}
